/**
 * 
 */
package deathKitten;

/**
 * @author Adam
 *
 */

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

import deathKitten.FileToByteArray;
import deathKitten.SHA512Generator;
import deathKitten.KeyMaker;

public class GUI extends JFrame{
	
	private byte[] targetFileBytes;
	private byte[] keyFileData1;
	private byte[] keyFileData2;
	private byte[] keyFileData3;
	private byte[] keyFileHash1;
	private byte[] keyFileHash2;
	private byte[] keyFileHash3;
	private byte[] passwordHash;
	
	public GUI() {
		 super("Death Kitten basic GUI");		 
		 setSize(350, 200);
		 setDefaultCloseOperation(EXIT_ON_CLOSE);

		 Container c = getContentPane();
		 c.setLayout(new FlowLayout());
		 
		 JButton targetFile = new JButton("Target File");
		 final JPasswordField passwordField = new JPasswordField(10);
		 JButton keyFile1 = new JButton("Key File 1");
		 JButton keyFile2 = new JButton("Key File 2");
		 JButton keyFile3 = new JButton("Key File 3");
		 JButton encryptButton = new JButton("Encrypt");
		 JButton decryptButton = new JButton("Decrypt");
		 final JLabel statusbar = 
		              new JLabel("Output of your selection will go here");

		 // Create a file chooser that opens up as an Open dialog
		 targetFile.addActionListener(new ActionListener() {			 
		   public void actionPerformed(ActionEvent ae) {
		     JFileChooser chooser = new JFileChooser();
		     chooser.setMultiSelectionEnabled(true);
		     int option = chooser.showOpenDialog(GUI.this);
		     if (option == JFileChooser.APPROVE_OPTION) {
		       File[] sf = chooser.getSelectedFiles();
		       String filelist = "nothing";
		       if (sf.length > 0) filelist = sf[0].getName();
		       for (int i = 1; i < sf.length; i++) {
		         filelist += ", " + sf[i].getName();
		       }
		       //////////
		       
		       try {
		    	   targetFileBytes = FileToByteArray.getBytesFromFile(sf[0]);
		       } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		       }
		       
		       //////////
		       statusbar.setText("You chose " + filelist);
		     }
		     else {
		       statusbar.setText("You canceled.");
		     }
		   }
		 });
	 
		 keyFile1.addActionListener(new ActionListener() {	
		   public void actionPerformed(ActionEvent ae) {
			     JFileChooser chooser = new JFileChooser();
			     chooser.setMultiSelectionEnabled(true);
			     int option = chooser.showOpenDialog(GUI.this);
			     if (option == JFileChooser.APPROVE_OPTION) {
			       File[] sf = chooser.getSelectedFiles();
			       String filelist = "nothing";
			       if (sf.length > 0) filelist = sf[0].getName();
			       for (int i = 1; i < sf.length; i++) {
			         filelist += ", " + sf[i].getName();
			       }
			       //////////
			       byte[] fileReturn = null;
			       try {
			    	   fileReturn = FileToByteArray.getBytesFromFile(sf[0]);
			    	   keyFileData1 = fileReturn;
			       } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			       }
			       
			    	  SHA512Generator hasher = new SHA512Generator();
			    	  try {
						keyFileHash1 = hasher.byteArraySHA512HashAsByteArray(fileReturn);
					} catch (NoSuchAlgorithmException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			       //////////
			       statusbar.setText("You chose " + filelist);
			     }
			     else {
			       statusbar.setText("You canceled.");
			     }
			   }
			 });
		 
		 keyFile2.addActionListener(new ActionListener() {	
			   public void actionPerformed(ActionEvent ae) {
				     JFileChooser chooser = new JFileChooser();
				     chooser.setMultiSelectionEnabled(true);
				     int option = chooser.showOpenDialog(GUI.this);
				     if (option == JFileChooser.APPROVE_OPTION) {
				       File[] sf = chooser.getSelectedFiles();
				       String filelist = "nothing";
				       if (sf.length > 0) filelist = sf[0].getName();
				       for (int i = 1; i < sf.length; i++) {
				         filelist += ", " + sf[i].getName();
				       }
				       //////////
				       byte[] fileReturn = null;
				       try {
				    	  fileReturn = FileToByteArray.getBytesFromFile(sf[0]);
				    	  keyFileData2 = fileReturn;
				       } catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
				       }
				       
				    	  SHA512Generator hasher = new SHA512Generator();
				    	  try {
				    		  keyFileHash2 = hasher.byteArraySHA512HashAsByteArray(fileReturn);
						} catch (NoSuchAlgorithmException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}				       
				       //////////
				       statusbar.setText("You chose " + filelist);
				     }
				     else {
				       statusbar.setText("You canceled.");
				     }
				   }
				 });
		 
		 keyFile3.addActionListener(new ActionListener() {	
				   public void actionPerformed(ActionEvent ae) {
					     JFileChooser chooser = new JFileChooser();
					     chooser.setMultiSelectionEnabled(true);
					     int option = chooser.showOpenDialog(GUI.this);
					     if (option == JFileChooser.APPROVE_OPTION) {
					       File[] sf = chooser.getSelectedFiles();
					       String filelist = "nothing";
					       if (sf.length > 0) filelist = sf[0].getName();
					       for (int i = 1; i < sf.length; i++) {
					         filelist += ", " + sf[i].getName();
					       }
					       //////////
					       byte[] fileReturn = null;
					       try {
					    	  fileReturn = FileToByteArray.getBytesFromFile(sf[0]);
					    	  keyFileData3 = fileReturn;
					       } catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					       }
					       
					    	  SHA512Generator hasher = new SHA512Generator();
					    	  try {
					    		  keyFileHash3 = hasher.byteArraySHA512HashAsByteArray(fileReturn);
							} catch (NoSuchAlgorithmException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					       //////////
					       statusbar.setText("You chose " + filelist);
					     }
					     else {
					       statusbar.setText("You canceled.");
					     }
					   }
					 });

		 // Create a file chooser that opens up as a Save dialog
		 encryptButton.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent ae) {
			   	///do encrypt action
			   SHA512Generator hasher = new SHA512Generator();
			   KeyMaker km = new KeyMaker();
			   
			   String pw = null;
			   byte[] passwordHash = null;
			   char[] password = passwordField.getPassword();
			   for (int i = 0; i < password.length; i++){
				   pw += password[i];
			   }
			   try {
				   passwordHash = hasher.textInToSHA512HashToByteArray(pw);
			   } catch (NoSuchAlgorithmException e) {
				   // TODO Auto-generated catch block
				   e.printStackTrace();
			   }
			byte[] keyForKeyScheduler = km.keyMaker(passwordHash, keyFileHash1, keyFileHash2, keyFileHash3);
			
			String hexStr = "";
			for (int i = 0; i < keyForKeyScheduler.length; i++) {
				hexStr +=  Integer.toString( ( keyForKeyScheduler[i] & 0xff ) + 0x100, 16).substring( 1 );
			}
			System.out.println(hexStr);

		   }
		 });

		 // Create a file chooser that allows you to pick a directory
		 // rather than a file
		 decryptButton.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent ae) {
			   	//do decrypt stuff
		   }
		 });

		 c.add(targetFile);
		 c.add(passwordField);
		 c.add(keyFile1);
		 c.add(keyFile2);
		 c.add(keyFile3);
		 c.add(encryptButton);
		 c.add(decryptButton);
		 c.add(statusbar);
		}
}
